package com.ontheroad.mysql.device.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.modoumama.service.base.impl.BaseServiceImpl;

import com.ontheroad.device.model.CentralDeviceEntity;
import com.ontheroad.device.model.DeviceStatusEntity;
import com.ontheroad.device.service.DeviceStatusTopicService;
import com.ontheroad.mysql.device.mapper.CentralDeviceTopicMapper;
import com.ontheroad.mysql.device.mapper.DeviceStatusTopicMapper;
import com.ontheroad.utils.UUIDUtil;

/*import com.ontheroad.utils.SmsUtil;*/

/**
 * Created by kedong on 2017/6/25 0021.
 */
@Service
@Transactional
public class DeviceStatusTopicServiceImpl extends BaseServiceImpl<DeviceStatusEntity, String>
		implements DeviceStatusTopicService {

	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private DeviceStatusTopicMapper deviceStatusTopicMapper;
	
	@Autowired
	private CentralDeviceTopicMapper centralDeviceTopicMapper;

	public HashMap<String, Object> insertDeviceStatusList(Boolean isSub,
			Integer channel, String mac, List<DeviceStatusEntity> destaList)
			throws Exception {
		HashMap<String, Object> bean  =new HashMap<String, Object>();
		log.info("添加设备状态信息");
		for(DeviceStatusEntity DeviceStatusEntity:destaList ){
			DeviceStatusEntity.setId(UUIDUtil.generate());
			DeviceStatusEntity.setUpdateTime(new Date());
			DeviceStatusEntity.setCreateTime(new Date());
			DeviceStatusEntity.setIsEnable(true);
			CentralDeviceEntity centralDeviceEntity=new CentralDeviceEntity();
			centralDeviceEntity.setChannel(channel);
			centralDeviceEntity.setMac(mac);
			centralDeviceEntity.setIsSub(isSub);
			centralDeviceEntity.setPort(DeviceStatusEntity.getPort());
			CentralDeviceEntity cenDeviceEntity=centralDeviceTopicMapper.findDevice(centralDeviceEntity);
			if(cenDeviceEntity==null){
				bean.put("code", 1);
				bean.put("message", "没有中控信息");
				return bean;
			}
			DeviceStatusEntity.setDeviceId(cenDeviceEntity.getDeviceId());
			DeviceStatusEntity.setType(cenDeviceEntity.getType());
		}
		deviceStatusTopicMapper.insertModel(destaList);
		bean.put("code", 0);
		bean.put("message", "success");
		return bean;
	}
}
